package com.bdqn.t350.ch06.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.t350.ch06.bean.User;
import com.bdqn.t350.ch06.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@RequestMapping("/user")
@Controller
public class UserController {

    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping("/getList")
    public Object getList(@RequestParam(name = "usrName", defaultValue = "") String usrName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("usr_name", usrName);
        return userService.selectList(queryWrapper);
    }

    @ResponseBody
    @RequestMapping("/selectPage")
    public Object selectPage(@RequestParam(name = "usrName", defaultValue = "") String usrName,
                             @RequestParam(name = "index", defaultValue = "1") int index) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("usr_name", usrName);
        IPage<User> page = new Page<>();
        page.setCurrent(index);
        userService.selectPage(page, queryWrapper);
        return page;
    }

    @ResponseBody
    @PostMapping("/insert")
    public Object insert(User user) {
        return userService.insert(user);
    }

    @ResponseBody
    @RequestMapping("/deleteById")
    public Object deleteById(int id) {
        return userService.deleteById(id);
    }


    @ResponseBody
    @PostMapping("/updateById")
    public Object updateById(User user) {
        return userService.updateById(user);
    }

}
